﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using Npgsql;
using BarFaculdade.Module;
using System.Data;

namespace BarFaculdade.DAL
{
    public class ClienteDAL
    {

        #region Declarações

        string strConn = ConfigurationManager.ConnectionStrings[2].ConnectionString;
        
        #endregion

        #region Métodos

        public DataSet BuscarClientes(int codigo, string nome)
        {

            #region Declarações

            NpgsqlConnection conn = null;
            NpgsqlCommand cmd = null;
            DataSet ds = null;
            NpgsqlDataAdapter da = null;
            NpgsqlCommandBuilder cmdBuilder = null;
            string sql = string.Empty;

            #endregion

            #region Implementação
            if (nome.Equals(""))
            {
                sql = @"Select * from ""Cliente"" where codigo = " + (int)codigo;
            }
            else if (nome.Length > 0 && !nome.Equals("todos"))
            {
                sql = @"Select * from ""Cliente"" where nome = '" + (string)nome + "';";
            }
            else
                sql = @"Select * from ""Cliente"";";

            ds = new DataSet();
            conn = new NpgsqlConnection(strConn);
            conn.Open();
            //cmd = new NpgsqlCommand(sql, conn);
            da = new NpgsqlDataAdapter(sql,conn);
            //cmdBuilder = new NpgsqlCommandBuilder(da);
            
            da.Fill(ds, "Cliente");

            return ds;

            #endregion
        }

        public bool Insert(Cliente cli, string sql)
        {
            #region Declaração
            NpgsqlConnection conn = null;
            NpgsqlCommand cmd = null;
            #endregion

            #region Implementação

            try
            {
                conn = new NpgsqlConnection(strConn);

                cmd = new NpgsqlCommand(sql, conn);

                cmd.ExecuteNonQuery();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                conn = null;
                cmd = null;
                sql = null;
            }

            #endregion

        }

        #endregion
    }
}
